package com.feng.controller.back;

import com.feng.dto.UserDto;
import com.feng.service.UserService;
import com.feng.util.JwtTokenUtil;
import com.feng.vo.PageVo;
import com.feng.vo.ResultVo;
import com.feng.vo.UserAddVo;
import com.feng.vo.UserVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author: 丰
 * @date: 2022/12/29 14:35
 */
@RestController    // Controller + ResponseBody
@RequestMapping("/back/user")
@Api(tags = "后台用户接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation("条件分页查询")
    @ApiResponses({@ApiResponse(code=0,message="请求成功")})
    @GetMapping("/getList")
    public ResultVo<List<UserDto>> getList(PageVo pageVo){
        return userService.selectAll(pageVo);
    }

    @ApiOperation("用户注册")
    @PutMapping("/register")
    public ResultVo<Integer> register(@RequestBody UserVo userVo, HttpServletRequest request){
        return userService.register(userVo, request);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultVo login(@RequestBody UserVo userVo, HttpServletRequest request){
        ResultVo<UserDto> resultVo = userService.login(userVo, request);
        if (resultVo.getCode() == 200){
            // 需要username，taken, tokenHead
            HashMap<String, String> data = new HashMap<>();
            data.put("username", resultVo.getData().getUsername());
            data.put("access_token", tokenHead + jwtTokenUtil.generateToken(resultVo.getData().getUsername()));
            return ResultVo.success(data);
        }
        return ResultVo.failed();
    }

    @ApiOperation("添加用户")
    @PutMapping("/addUser")
    public ResultVo<Integer> addUser(@RequestBody UserAddVo userVo){
        return userService.insert(userVo);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/getById/{id}")
    public ResultVo<UserDto> getById(@PathVariable("id") Integer id){
        return userService.selectById(id);
    }

    @ApiOperation("修改用户")
    @PostMapping("/update")
    public ResultVo<Integer> update(@RequestBody UserAddVo userAddVo){
        return userService.update(userAddVo);
    }

    @ApiOperation("更改密码")
    @PostMapping("/updatePassword")
    public ResultVo<Integer> updatePassword(@RequestBody UserVo userVo){
        return userService.updatePassword(userVo);
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/deleteById/{id}")
    public ResultVo<Integer> deleteById(@PathVariable("id") Integer id){
        return userService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteAll")
    public ResultVo<Integer> deleteAll(@RequestBody Integer[] ids){
        return userService.deleteAll(ids);
    }

}
